// pages/orderfood/showWant/index.js


Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDescription: '',
    orderContent: '',
    addName: '',
    addPhoneno: '',
    addAddress: ''
  },
  setStor: function () {
    console.log(wx.getStorageSync("orderContent"));
  },
  orderOK: function () {
    let orderContent = this.data.orderContent;
    let orderDescription = this.data.orderDescription;
    let addName = this.data.addName;
    let addPhoneno = this.data.addPhoneno;
    let addAddress = this.data.addAddress;
    let userID = wx.getStorageSync("cmkc_user_id");
    // 发送请求
    let ru = getApp().globalData.requestUrl + "5c7a2f29b204d";
    console.log(userID);
    wx.request({
      url: ru,
      data: {
        cmkc_user_id: userID,
        order_content: orderContent,
        order_description: orderDescription,
        add_name: addName,
        add_phoneno: addPhoneno,
        add_address: addAddress
      },
      header: { version: "v3.0" },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        console.log(res);
        if (res.data.code == 1) {
          let orderID = res.data.data.order_id
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            success(res) {
              wx.navigateTo({
                url: "/pages/center/order/ordershow/index?order_id=" + orderID,
              })
            }
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    options.fromPage == "choseAddress" ? this.updateAddress(false, options.address_id) : this.updateAddress(true);
    this.updateShow();
  },
  updateShow: function () {
    console.log("getstasdfasd", wx.getStorageSync("orderContent"));
    this.setData({
      orderContent: wx.getStorageSync("orderContent"),
      orderDescription: wx.getStorageSync("orderDescription")
    });
  },
  updateAddress: function (isDefault, addressID) {
    let userID = wx.getStorageSync("cmkc_user_id");
    console.log("isDefault", isDefault);
    let _this = this;
    wx.showLoading({
      title: '正在加载',
    });
    let adStatus = isDefault ? "get_default_address" : "get_address_by_id";
    console.log("adStatus", adStatus);
    //获取默认地址
    let ru = getApp().globalData.requestUrl + "5c833429a2445";
    wx.request({
      url: ru,
      data: {
        status: adStatus,
        cmkc_user_id: userID,
        address_id: addressID
      },
      header: { version: 'v3.0' },
      method: 'POST',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 1) {
          let addressInfo = res.data.data.address_info;
          _this.setData({
            addName: addressInfo.name,
            addPhoneno: addressInfo.phoneno,
            addAddress: addressInfo.address
          });
        } else {
          wx.showModal({
            title: '提示',
            content: "您还没有添加默认地址,请添加。",
            showCancel: false,
            success(res) {
              console.log(res);
              wx.switchTab({
                url: '/pages/center/index',
              })
            }
          })
        }
      }
    })
  }
})